//==================================================================
//  Copyright (C) 2006  Davide Pasca
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//==================================================================
///
///
///
///
///
//==================================================================

#ifndef PGAMEPAD_H
#define PGAMEPAD_H

#include "psys.h"

//==================================================================
///
//==================================================================
class PGamePad
{
public:
	static const int	MAX_PADS			= 4;

	static const int	DPAD_UP				= 0x0001;
	static const int	DPAD_DOWN			= 0x0002;
	static const int	DPAD_LEFT			= 0x0004;
	static const int	DPAD_RIGHT			= 0x0008;
	static const int	BUTT_START			= 0x0010;
	static const int	BUTT_BACK			= 0x0020;
	static const int	LEFT_THUMB			= 0x0040;
	static const int	RIGHT_THUMB			= 0x0080;
	static const int	LEFT_SHOULDER		= 0x0100;
	static const int	RIGHT_SHOULDER		= 0x0200;
	static const int	BUTT_A				= 0x1000;
	static const int	BUTT_B				= 0x2000;
	static const int	BUTT_X				= 0x4000;
	static const int	BUTT_Y				= 0x8000;

	static const int	LEFT_TRIGGER		= 0x10000;
	static const int	RIGHT_TRIGGER		= 0x20000;

	static const int	DPAD_ALL			= 0x000f;
	
private:
	u_int	_buttons[MAX_PADS];
	float	_sticks[MAX_PADS][2][2];
	void	*_contextp;

	u_int	_old_buttons[MAX_PADS];
public:
	PGamePad( void *contextp=NULL );

	void	Update();
	u_int	GetButtons( int pad_idx ) const	{	return _buttons[pad_idx];		}
	void	GetSticks( int pad_idx, float *out_leftp, float *out_rightp ) const;
	bool	IsConnected( int pad_idx ) const;

	u_int	GetChangedButtons( int pad_idx ) const
	{
		return (_buttons[pad_idx] & (~_old_buttons[pad_idx])) & _buttons[pad_idx];
	}

private:
	u_int	mapButtons( u_int inbuttons );
};

#endif
